# coding:utf-8
import os

def get_files(path='D:\zyt\\azyt\sfx', rule=".sfx"):
    all = []
    for fpathe,dirs,fs in os.walk(path):   # os.walk获取所有的目录
        for f in fs:
            filename = os.path.join(fpathe,f)
            if filename.endswith(rule):  # 判断是否是".sfx"结尾
                all.append(filename)
    return all

def file_insert(path, lenth,rule, string):
    lines = []
    with open(path, encoding='UTF-8') as f:
        lines = f.readlines()
    f.close()

    if lines != []:
        if rule in lines[lenth]:
            print('已经有了')
        else:
            with open(path, 'w', encoding='UTF-8') as f:
                print('插入行')
                lines.insert(lenth,string)
                f.writelines(lines)
            f.close()

htmlpath = get_files(r".", rule=".html")
for i in htmlpath:
    print(i)
    file_insert(i,7,r'referrer',r'<meta name="referrer" content="no-referrer" />')

import os
os.system('pause') #按任意键继续
